% May 23, 2025
% Duffy, Lebeau, Puzzello

% This file replicates Figures 5, 6, 7, 8, 9, 11, 12 and E1.

%% Set directory HERE
% Example: cd 'C:\Users\Lenovo\Desktop\replication_DLP\utility'
cd 'C:\...\replication_DLP\utility'
 
%% Load and convert data
opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=30";
opts.DataRange = "A1:J657";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m30AcceptedTable = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=30, 1-15";
opts.DataRange = "A1:J657";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m30AcceptedTableHalf1 = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=30, 16-30";
opts.DataRange = "A1:J657";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m30AcceptedTableHalf2 = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=60";
opts.DataRange = "A1:J675";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m60AcceptedTable = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=60, 1-15";
opts.DataRange = "A1:J675";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m60AcceptedTableHalf1 = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=60, 16-30";
opts.DataRange = "A1:J675";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m60AcceptedTableHalf2 = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=315";
opts.DataRange = "A1:J699";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m315AcceptedTable = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=135, 1-15";
opts.DataRange = "A1:J699";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m315AcceptedTableHalf1 = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

opts = spreadsheetImportOptions("NumVariables", 10);
opts.Sheet = "m=135, 16-30";
opts.DataRange = "A1:J699";
opts.VariableNames = ["Var1", "Var2", "quantity", "tokens", "Var5", "Var6", "Var7", "Var8", "buyersurplus", "sellersurplus"];
opts.SelectedVariableNames = ["quantity", "tokens", "buyersurplus", "sellersurplus"];
opts.VariableTypes = ["char", "char", "double", "double", "char", "char", "char", "char", "double", "double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var5", "Var6", "Var7", "Var8"], "EmptyFieldRule", "auto");
m315AcceptedTableHalf2 = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

m315ag = table2array(m315AcceptedTable);
m60ag = table2array(m60AcceptedTable);
m30ag = table2array(m30AcceptedTable);

m315agh1 = table2array(m315AcceptedTableHalf1);
m60agh1 = table2array(m60AcceptedTableHalf1);
m30agh1 = table2array(m30AcceptedTableHalf1);

m315agh2 = table2array(m315AcceptedTableHalf2);
m60agh2 = table2array(m60AcceptedTableHalf2);
m30agh2 = table2array(m30AcceptedTableHalf2);

opts = spreadsheetImportOptions("NumVariables", 13);
opts.Sheet = "p=1,m=315";
opts.DataRange = "A1:M699";
opts.VariableNames = ["round", "session", "quantity", "tokens", "agreement", "recorded", "cost", "utility", "buyersurplus", "sellersurplus", "totalsurplus", "treatment", "prob"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
prob1AcceptedTable = readtable("accepted_offers.xlsx", opts, "UseExcel", false);
clear opts

opts = spreadsheetImportOptions("NumVariables", 13);
opts.Sheet = "p=0.5";
opts.DataRange = "A1:M365";
opts.VariableNames = ["round", "session", "quantity", "tokens", "agreement", "recorded", "cost", "utility", "buyersurplus", "sellersurplus", "totalsurplus", "treatment", "prob"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "string", "string", "string", "double", "double", "double", "double", "double"];
opts = setvaropts(opts, ["recorded", "cost", "utility"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["recorded", "cost", "utility"], "EmptyFieldRule", "auto");
prob05AcceptedTable = readtable("accepted_offers.xlsx", opts, "UseExcel", false);
clear opts

opts = spreadsheetImportOptions("NumVariables", 13);
opts.Sheet = "p=0.25";
opts.DataRange = "A1:M404";
opts.VariableNames = ["round", "session", "quantity", "tokens", "agreement", "recorded", "cost", "utility", "buyersurplus", "sellersurplus", "totalsurplus", "treatment", "prob"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "string", "string", "string", "double", "double", "double", "double", "double"];
opts = setvaropts(opts, ["recorded", "cost", "utility"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["recorded", "cost", "utility"], "EmptyFieldRule", "auto");
prob025AcceptedTable = readtable("accepted_offers.xlsx", opts, "UseExcel", false);

p1ag = table2array(prob1AcceptedTable);
p05ag = table2array(prob05AcceptedTable);
p025ag = table2array(prob025AcceptedTable);

p1agBuyerShare=p1ag(:,9)./p1ag(:,11);
p05agBuyerShare=str2double(p05ag(:,9))./str2double(p05ag(:,11));
p025agBuyerShare=str2double(p025ag(:,9))./str2double(p025ag(:,11));

%% Options and colors
map = brewermap(6,'Set1'); 
set(0,'DefaultFigureColor',[1 1 1]);
color(1,:)=map(1,:);
color(2,:)=map(5,:);
color(3,:)=map(3,:);
%% Figure 5

m315agTotalSurplus=m315ag(:,3)+m315ag(:,4);
m315agBuyerShare=m315ag(:,3)./m315agTotalSurplus;

fig1=figure(1);
tiledlayout(1,2); 
nexttile;
edges1 = [0.355:0.01:0.655];
h1=histogram(m315agBuyerShare,edges1,'Normalization','probability','facecolor',map(3,:),'facealpha',.6,'edgecolor',map(3,:));
xlabel("S^b/S");
title('Distribution')
xlim([0.35,0.65]);
axis tight
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);
nexttile;
h(3,1)=cdfplot(m315agBuyerShare)
xlim([0.35,0.65]);
set( h(3,1), 'Color', map(3,:),'LineWidth',2.5);
axis tight
xlabel("S^b/S");
ylabel('');
xticks([0.25 0.5 0.75])
title('Cum. Distribution')
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);
%% Figure 6
fig1=figure(1);
tiledlayout(2,1); 
nexttile;
edges2 = [0.05:0.1:7];
h1=histogram(m315ag(:,1),edges2,'Normalization','probability','facecolor',map(3,:),'facealpha',.6,'edgecolor',map(3,:));
hold on;
histogram(m60ag(:,1),edges2,'Normalization','probability','facecolor',map(5,:),'facealpha',.6,'edgecolor',map(5,:));
hold on;
histogram(m30ag(:,1),edges2,'Normalization','probability','facecolor',map(1,:),'facealpha',.6,'edgecolor',map(1,:));
box off
axis tight
xNash315b=xline(4, '--', 'FontName','Garamond', 'Color',map(3,:), 'LineWidth',2,'Alpha',1);
xNash315b.LabelOrientation = 'horizontal';
xKalai315b=xline(4, ':', 'FontName','Garamond', 'Color',map(3,:), 'LineWidth',2,'Alpha',1);
xKalai315b.LabelOrientation = 'horizontal';

xNash60b=xline(2.17, '--', 'FontName','Garamond', 'Color',map(5,:), 'LineWidth',2,'Alpha',1);
xNash60b.LabelOrientation = 'horizontal';
xKalai60b=xline(1.69, ':', 'FontName','Garamond', 'Color',map(5,:), 'LineWidth',2,'Alpha',1);
xKalai60b.LabelOrientation = 'horizontal';

xNash30b=xline(1.24, '--', 'FontName','Garamond', 'Color',map(1,:), 'LineWidth',2,'Alpha',1);
xNash30b.LabelOrientation = 'horizontal';
xKalai30b=xline(0.63, ':', 'FontName','Garamond', 'Color',map(1,:), 'LineWidth',2,'Alpha',1);
xKalai30b.LabelOrientation = 'horizontal';

xlim([0, 7]);
xlabel('q');
title('Distribution of traded quantities')
legend('m = 315','m = 60','m = 30','Nash prediction', 'Kalai prediction','location','northeast')
legend boxoff
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);

nexttile;
edges2 = [1.41:5:200];
edges3 = [1.41:5:56.41 60];
edges4 = [1.41:5:26.41 30];
histogram(m315ag(:,2),edges2,'Normalization','probability','facecolor',map(3,:),'facealpha',.7,'edgecolor',map(3,:));
hold on;
edges5 = [5:5:60];
histogram(m60ag(:,2),edges3,'Normalization','probability','facecolor',map(5,:),'facealpha',.7,'edgecolor',map(5,:));
hold on;
edges6 = [10:5:30];
histogram(m30ag(:,2),edges4,'Normalization','probability','facecolor',map(1,:),'facealpha',.7,'edgecolor',map(1,:));
box off

xNash315=xline(118.91, '--', 'FontName','Garamond', 'Color',map(3,:), 'LineWidth',2,'Alpha',1);
xNash315.LabelOrientation = 'horizontal';
xKalai315=xline(118.91, ':', 'FontName','Garamond', 'Color',map(3,:), 'LineWidth',2,'Alpha',1);
xKalai315.LabelOrientation = 'horizontal';

xNash60=xline(60, '--', 'FontName','Garamond', 'Color',map(5,:), 'LineWidth',2,'Alpha',1);
xNash60.LabelOrientation = 'horizontal';
xKalai60=xline(60, ':', 'FontName','Garamond', 'Color',map(5,:), 'LineWidth',2,'Alpha',1);
xKalai60.LabelOrientation = 'horizontal';

xNash30=xline(30, '--', 'FontName','Garamond', 'Color',map(1,:), 'LineWidth',2,'Alpha',1);
xNash30.LabelOrientation = 'horizontal';
xKalai30=xline(30, ':', 'FontName','Garamond', 'Color',map(1,:), 'LineWidth',2,'Alpha',1);
xKalai30.LabelOrientation = 'horizontal';

axis tight
xlim([0, 200]);
xlabel('y');
title('Distribution of traded tokens')
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);
%% Figure 7
fig2=figure(2);
tiledlayout(1,2);  
nexttile;
h(1,1)=cdfplot(m315ag(:,1))
hold on;
h(1,2)=cdfplot(m60ag(:,1))
hold on;
h(1,3)=cdfplot(m30ag(:,1))
set( h(1,1), 'Color', map(3,:),'LineWidth',2.5);
set( h(1,2), 'Color', map(5,:),'LineWidth',2.5);
set( h(1,3), 'Color', map(1,:),'LineWidth',2.5);

xlim([0, 7]);
ylim([0, 1]);
xlabel('q');
ylabel('');

title('Cumulative distribution of traded quantities')
legend('m = 315','m = 60','m = 30','location','southeast')
legend boxoff
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);

box off
axis tight

nexttile;
h(2,1)=cdfplot(m315ag(:,2))
hold on;
h(2,2)=cdfplot(m60ag(:,2))
hold on;
h(2,3)=cdfplot(m30ag(:,2))
set( h(2,1), 'Color', map(3,:),'LineWidth',2.5);
set( h(2,2), 'Color', map(5,:),'LineWidth',2.5);
set( h(2,3), 'Color', map(1,:),'LineWidth',2.5);

xlim([0, 200]);
ylim([0, 1]);
xlabel('y');
ylabel('');

title('Cumulative distribution of traded tokens')
set(gca,'Color','white','FontName','Garamond','LineWidth',1.5,'FontWeight','bold','FontSize',14);
box off
axis tight
%% Figure 8, left panel
figure
f1=scatter(m30ag(:,4),m30ag(:,3),50, 'MarkerFaceColor',color(1,:),'MarkerEdgeColor',color(1,:));
hold on
f2=scatter(m60ag(:,4),m60ag(:,3),50, 'MarkerFaceColor',color(2,:),'MarkerEdgeColor',color(2,:));
hold on
f3=scatter(m315ag(:,4),m315ag(:,3), 50,'MarkerFaceColor',color(3,:),'MarkerEdgeColor',color(3,:));
axis([0 100 0 100]);
f1.MarkerFaceAlpha = 0.2;
f2.MarkerFaceAlpha = 0.2;
f3.MarkerFaceAlpha = 0.2;
xticks([100]);
yticks([100]);
xticklabels({});
yticklabels({});
hAxes = gca; 
hAxes.XRuler.Axle.LineStyle = 'none';  
hAxes.YRuler.Axle.LineStyle = 'none';
axis square;
%Overlayed with Pareto graph by hand
%% Figure 8
figure
g1=scatter(m30ag(:,1),m30ag(:,2),50, 'MarkerFaceColor',color(1,:),'MarkerEdgeColor',color(1,:));
hold on
g2=scatter(m60ag(:,1),m60ag(:,2),50, 'MarkerFaceColor',color(2,:),'MarkerEdgeColor',color(2,:));
hold on
g3=scatter(m315ag(:,1),m315ag(:,2), 50,'MarkerFaceColor',color(3,:),'MarkerEdgeColor',color(3,:));
axis([0 5 0 200]);
g1.MarkerFaceAlpha = 0.2;
g2.MarkerFaceAlpha = 0.2;
g3.MarkerFaceAlpha = 0.2;
xticks([5]);
yticks([200]);
xticklabels({});
yticklabels({});
hAxes = gca; 
hAxes.XRuler.Axle.LineStyle = 'none';  
hAxes.YRuler.Axle.LineStyle = 'none';
axis square;
%Overlayed with utility and cost functions by hand
%% Figure 9: left panel
color(1,:)=map(1,:);
color(2,:)=map(5,:);
color(3,:)=map(3,:);
X = categorical({'m = 30','m = 60','m = 135'});
X = reordercats(X,{'m = 30','m = 60','m = 135'});
Y = [28.11 42.14 51.09];
conf = [0.69 0.69 0.28];

figure4=figure(4)
b=bar(X,Y,0.4, 'FaceColor','flat', 'EdgeColor','flat', 'LineWidth',2)
b.CData = color;
alpha(b,0.7)
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = string(b(1).YData);
t=text(xtips1,ytips1,labels1,'HorizontalAlignment','right', 'VerticalAlignment','bottom', 'FontName','Garamond','FontWeight','bold','FontSize',12)
hold on
nash=plot(X,[54.4,58.19,51.5],'-', 'LineWidth', 2,'Color','black')
hold on
kalai=plot(X,[26.96,41.71,51.5],'--', 'LineWidth', 2,'Color','black')
hold on
er = errorbar(X,Y,conf,conf);   
er.Color = [0 0 0];                            
er.LineStyle = 'none';
er.LineWidth=1.5;
ylabel('S^b')
box off
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);
title("Mean buyer surplus")
text(1.2,36,'Kalai','FontName','Garamond','FontWeight','bold','FontSize',14)
text(0.8,57,'Nash','FontName','Garamond','FontWeight','bold','FontSize',14)
x0=10;
y0=10;
width=550;
height=400
set(gcf,'position',[x0,y0,width,height])
%% Figure 9: right panel
h(3,1)=cdfplot(m315ag(:,3))
hold on;
h(3,2)=cdfplot(m60ag(:,3))
hold on;
h(3,3)=cdfplot(m30ag(:,3))
set( h(3,1), 'Color', map(3,:),'LineWidth',2.5);
set( h(3,2), 'Color', map(5,:),'LineWidth',2.5);
set( h(3,3), 'Color', map(1,:),'LineWidth',2.5);
xlabel('S^b');
ylabel('');
title('Cumulative distribution of buyer surplus')
legend('m = 315','m = 60','m = 30','location','southeast')
legend boxoff
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);
box off
axis tight
x0=10;
y0=10;
width=550;
height=420
set(gcf,'position',[x0,y0,width,height])
%% Figure 11
edges1 = [0.00:0.015:1.00];
edges2 = [0.005:0.015:1.005];
edges3 = [0.010:0.015:1.010];
figure
tiledlayout(1,2)
nexttile;
histogram(p1agBuyerShare,edges3,'Normalization','probability','facecolor',map(3,:),'facealpha',0.4,'edgecolor',map(3,:),'LineWidth',1);
hold on
histogram(p05agBuyerShare,edges1,'Normalization','probability','facecolor',map(4,:),'facealpha',0.6,'edgecolor',map(4,:), 'LineStyle','--', 'LineWidth',1.5);
hold on
histogram(p025agBuyerShare,edges2,'Normalization','probability','facecolor',map(2,:),'facealpha',0.6,'edgecolor',map(2,:), 'LineStyle',':','LineWidth',1.5);
xlim([0.4,0.7])
ylim([0,0.8])
xlabel("S^b/S");
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);
title("Distribution of the buyer's share")
legend('p=1','p = 0.5','p = 0.25','location','northeast')
legend boxoff
nexttile;
h(10,1)=cdfplot(p1agBuyerShare)
hold on
h(10,2)=cdfplot(p05agBuyerShare)
hold on
h(10,3)=cdfplot(p025agBuyerShare)
set( h(10,1), 'Color', map(3,:),'LineWidth',2.5);
set( h(10,2),'LineStyle', '--','Color', map(4,:),'LineWidth',2.5);
set( h(10,3),'LineStyle', ':','Color', map(2,:),'LineWidth',2.5);
xlim([0.2, 0.888]);
%ylim([0, 1]);
xlabel('S^b/S');
ylabel('');
title("Cumulative distribution of the buyer's share")
legend('p = 1','p = 0.5','p = 0.25','location','southeast')
legend boxoff
set(gca,'Color','white','FontName','Garamond','LineWidth',1.2,'FontWeight','bold','FontSize',14);

%% Figure 12
opts = spreadsheetImportOptions("NumVariables", 14);
opts.Sheet = "BuyersShares";
opts.DataRange = "A1:N286";
opts.VariableNames = ["last315", "x2ndtolast315", "x3rdToLast315", "x4thToLast315", "VarName5", "last60", "x2ndtolast60", "x3rdToLast60", "x4thToLast60", "VarName10", "last30", "x2ndtolast30", "x3rdToLast30", "x4thToLast30"];
opts.VariableTypes = ["double", "double", "double", "double", "string", "double", "double", "double", "double", "string", "double", "double", "double", "double"];
opts = setvaropts(opts, ["VarName5", "VarName10"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["VarName5", "VarName10"], "EmptyFieldRule", "auto");
tbl = readtable("convergence.xlsx", opts, "UseExcel", false);

last315 = tbl.last315;
x2ndtolast315 = tbl.x2ndtolast315;
x3rdToLast315 = tbl.x3rdToLast315;
x4thToLast315 = tbl.x4thToLast315;
VarName5 = tbl.VarName5;
last60 = tbl.last60;
x2ndtolast60 = tbl.x2ndtolast60;
x3rdToLast60 = tbl.x3rdToLast60;
x4thToLast60 = tbl.x4thToLast60;
VarName10 = tbl.VarName10;
last30 = tbl.last30;
x2ndtolast30 = tbl.x2ndtolast30;
x3rdToLast30 = tbl.x3rdToLast30;
x4thToLast30 = tbl.x4thToLast30;
clear opts tbl

figure
% m = 30
subplot(1,3,1)
scatter(6*ones(length(last30),1), last30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(6, nanmedian(last30), 90, 's', 'black', 'linewidth', 1.5)
scatter(6, nanmean(last30), 70, 'x', 'black', 'linewidth', 1.5)
scatter(5*ones(length(x2ndtolast30),1), x2ndtolast30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(5, nanmedian(x2ndtolast30), 90, 's', 'black', 'linewidth', 1.5)
scatter(5, nanmean(x2ndtolast30), 70, 'x', 'black', 'linewidth', 1.5)
scatter(4*ones(length(x3rdToLast30),1), x3rdToLast30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(4, nanmedian(x3rdToLast30), 90, 's', 'black', 'linewidth', 1.5)
scatter(4, nanmean(x3rdToLast30), 70, 'x', 'black', 'linewidth', 1.5)
scatter(3*ones(length(x4thToLast30),1), x4thToLast30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(3, nanmedian(x4thToLast30), 90, 's', 'black', 'linewidth', 1.5)
scatter(3, nanmean(x4thToLast30), 70, 'x', 'black', 'linewidth', 1.5)
yline(0.5, ':', 'LineWidth', 2)  % Kalai prediction
title('m = 30')
xlabel("Seller's Offer")
ylabel("Seller's surplus")
xticks([3 4 5 6])
ylim([0.35 0.65])
xticklabels({'4th to last','3rd to last','2nd to last','Last'})
set(gca, 'Color', 'white', 'FontName', 'Garamond', 'LineWidth', 1.2, 'FontWeight', 'bold', 'FontSize', 10)
% m = 60
subplot(1,3,2)
scatter(6*ones(length(last60),1), last60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(6, nanmedian(last60), 90, 's', 'black', 'linewidth', 1.5)
scatter(6, nanmean(last60), 70, 'x', 'black', 'linewidth', 1.5)
scatter(5*ones(length(x2ndtolast60),1), x2ndtolast60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(5, nanmedian(x2ndtolast60), 90, 's', 'black', 'linewidth', 1.5)
scatter(5, nanmean(x2ndtolast60), 70, 'x', 'black', 'linewidth', 1.5)
scatter(4*ones(length(x3rdToLast60),1), x3rdToLast60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(4, nanmedian(x3rdToLast60), 90, 's', 'black', 'linewidth', 1.5)
scatter(4, nanmean(x3rdToLast60), 70, 'x', 'black', 'linewidth', 1.5)
scatter(3*ones(length(x4thToLast60),1), x4thToLast60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(3, nanmedian(x4thToLast60), 90, 's', 'black', 'linewidth', 1.5)
scatter(3, nanmean(x4thToLast60), 70, 'x', 'black', 'linewidth', 1.5)
yline(0.5, ':', 'LineWidth', 2)
title('m = 60')
xlabel("Seller's Offer")
ylabel("Seller's surplus")
xticks([3 4 5 6])
ylim([0.35 0.65])
xticklabels({'4th to last','3rd to last','2nd to last','Last'})
set(gca, 'Color', 'white', 'FontName', 'Garamond', 'LineWidth', 1.2, 'FontWeight', 'bold', 'FontSize', 10)
% m = 315
subplot(1,3,3)
scatter(6*ones(length(last315),1), last315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(6, nanmedian(last315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(6, nanmean(last315), 70, 'x', 'black', 'linewidth', 1.5)
hold on
scatter(5*ones(length(x2ndtolast315),1), x2ndtolast315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(5, nanmedian(x2ndtolast315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(5, nanmean(x2ndtolast315), 70, 'x', 'black', 'linewidth', 1.5)
hold on
scatter(4*ones(length(x3rdToLast315),1), x3rdToLast315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(4, nanmedian(x3rdToLast315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(4, nanmean(x3rdToLast315), 70, 'x', 'black', 'linewidth', 1.5)
hold on
scatter(3*ones(length(x4thToLast315),1), x4thToLast315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(3, nanmedian(x4thToLast315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(3, nanmean(x4thToLast315), 70, 'x', 'black', 'linewidth', 1.5)
yline(0.50, ':', 'LineWidth', 2)
title('m = 315')
xlabel("Buyer's Offer")
ylabel("Buyer's share of surplus")
xticks([3 4 5 6])
ylim([0.35 0.65])
xticklabels({'4th to last','3rd to last','2nd to last','Last'})
set(gca, 'Color', 'white', 'FontName', 'Garamond', 'LineWidth', 1.2, 'FontWeight', 'bold', 'FontSize', 10)


%% Figure E1
opts = spreadsheetImportOptions("NumVariables", 14);
opts.Sheet = "SellersShares";
opts.DataRange = "A1:N286";
opts.VariableNames = ["last315", "x2ndtolast315", "x3rdToLast315", "x4thToLast315", "VarName5", "last60", "x2ndtolast60", "x3rdToLast60", "x4thToLast60", "VarName10", "last30", "x2ndtolast30", "x3rdToLast30", "x4thToLast30"];
opts.VariableTypes = ["double", "double", "double", "double", "string", "double", "double", "double", "double", "string", "double", "double", "double", "double"];
opts = setvaropts(opts, ["VarName5", "VarName10"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["VarName5", "VarName10"], "EmptyFieldRule", "auto");
tbl = readtable("convergence.xlsx", opts, "UseExcel", false);

last315 = tbl.last315;
x2ndtolast315 = tbl.x2ndtolast315;
x3rdToLast315 = tbl.x3rdToLast315;
x4thToLast315 = tbl.x4thToLast315;
VarName5 = tbl.VarName5;
last60 = tbl.last60;
x2ndtolast60 = tbl.x2ndtolast60;
x3rdToLast60 = tbl.x3rdToLast60;
x4thToLast60 = tbl.x4thToLast60;
VarName10 = tbl.VarName10;
last30 = tbl.last30;
x2ndtolast30 = tbl.x2ndtolast30;
x3rdToLast30 = tbl.x3rdToLast30;
x4thToLast30 = tbl.x4thToLast30;
clear opts tbl

figure
% m = 30
subplot(1,3,1)
scatter(6*ones(length(last30),1), last30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(6, nanmedian(last30), 90, 's', 'black', 'linewidth', 1.5)
scatter(6, nanmean(last30), 70, 'x', 'black', 'linewidth', 1.5)
scatter(5*ones(length(x2ndtolast30),1), x2ndtolast30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(5, nanmedian(x2ndtolast30), 90, 's', 'black', 'linewidth', 1.5)
scatter(5, nanmean(x2ndtolast30), 70, 'x', 'black', 'linewidth', 1.5)
scatter(4*ones(length(x3rdToLast30),1), x3rdToLast30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(4, nanmedian(x3rdToLast30), 90, 's', 'black', 'linewidth', 1.5)
scatter(4, nanmean(x3rdToLast30), 70, 'x', 'black', 'linewidth', 1.5)
scatter(3*ones(length(x4thToLast30),1), x4thToLast30, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(3, nanmedian(x4thToLast30), 90, 's', 'black', 'linewidth', 1.5)
scatter(3, nanmean(x4thToLast30), 70, 'x', 'black', 'linewidth', 1.5)
yline(0.5, ':', 'LineWidth', 2)  % Kalai prediction
title('m = 30')
xlabel("Seller's Offer")
ylabel("Seller's share of surplus")
xticks([3 4 5 6])
ylim([0.35 0.65])
xticklabels({'4th to last','3rd to last','2nd to last','Last'})
set(gca, 'Color', 'white', 'FontName', 'Garamond', 'LineWidth', 1.2, 'FontWeight', 'bold', 'FontSize', 10)
% m = 60
subplot(1,3,2)
scatter(6*ones(length(last60),1), last60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(6, nanmedian(last60), 90, 's', 'black', 'linewidth', 1.5)
scatter(6, nanmean(last60), 70, 'x', 'black', 'linewidth', 1.5)
scatter(5*ones(length(x2ndtolast60),1), x2ndtolast60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(5, nanmedian(x2ndtolast60), 90, 's', 'black', 'linewidth', 1.5)
scatter(5, nanmean(x2ndtolast60), 70, 'x', 'black', 'linewidth', 1.5)
scatter(4*ones(length(x3rdToLast60),1), x3rdToLast60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(4, nanmedian(x3rdToLast60), 90, 's', 'black', 'linewidth', 1.5)
scatter(4, nanmean(x3rdToLast60), 70, 'x', 'black', 'linewidth', 1.5)
scatter(3*ones(length(x4thToLast60),1), x4thToLast60, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
scatter(3, nanmedian(x4thToLast60), 90, 's', 'black', 'linewidth', 1.5)
scatter(3, nanmean(x4thToLast60), 70, 'x', 'black', 'linewidth', 1.5)
yline(0.5, ':', 'LineWidth', 2)
title('m = 60')
xlabel("Seller's Offer")
ylabel("Seller's surplus")
xticks([3 4 5 6])
ylim([0.35 0.65])
xticklabels({'4th to last','3rd to last','2nd to last','Last'})
set(gca, 'Color', 'white', 'FontName', 'Garamond', 'LineWidth', 1.2, 'FontWeight', 'bold', 'FontSize', 10)
% m = 315
subplot(1,3,3)
scatter(6*ones(length(last315),1), last315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(6, nanmedian(last315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(6, nanmean(last315), 70, 'x', 'black', 'linewidth', 1.5)
hold on
scatter(5*ones(length(x2ndtolast315),1), x2ndtolast315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(5, nanmedian(x2ndtolast315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(5, nanmean(x2ndtolast315), 70, 'x', 'black', 'linewidth', 1.5)
hold on
scatter(4*ones(length(x3rdToLast315),1), x3rdToLast315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(4, nanmedian(x3rdToLast315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(4, nanmean(x3rdToLast315), 70, 'x', 'black', 'linewidth', 1.5)
hold on
scatter(3*ones(length(x4thToLast315),1), x4thToLast315, 'filled', 'MarkerFaceAlpha', .2, 'MarkerEdgeAlpha', .2)
hold on
scatter(3, nanmedian(x4thToLast315), 90, 's', 'black', 'linewidth', 1.5)
hold on
scatter(3, nanmean(x4thToLast315), 70, 'x', 'black', 'linewidth', 1.5)
yline(0.50, ':', 'LineWidth', 2)
title('m = 315')
xlabel("Seller's Offer")
ylabel("Seller's surplus")
xticks([3 4 5 6])
ylim([0.35 0.65])
xticklabels({'4th to last','3rd to last','2nd to last','Last'})
set(gca, 'Color', 'white', 'FontName', 'Garamond', 'LineWidth', 1.2, 'FontWeight', 'bold', 'FontSize', 10)

